package com.study.shop.config.web;



import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

@Configuration
@ConfigurationProperties(prefix = "auth")
@Setter
public class WebConfig implements WebMvcConfigurer {
    @Resource
    private UserInterceptor userInterceptor;

    private List<String> paths;

    /**
     * 配置跨域
     */
    // @Override
    // public void addCorsMappings(CorsRegistry registry) {
    //     registry.addMapping("/**")  // 所有接口
    //             // .allowedOrigins("*")  // 允许所有来源（生产环境建议指定）
    //             .allowedOriginPatterns("*")
    //             .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
    //             .allowedHeaders("*")
    //             .allowCredentials(true)  // 允许携带 Cookie（需配合指定 origin）
    //             .maxAge(3600);  // 预检请求缓存时间
    // }

    /**
     * 配置拦截器
     *
     * @param registry registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration registration = registry.addInterceptor(userInterceptor);
        if (!CollectionUtils.isEmpty(paths)) {
            System.out.println(paths);
            registration.excludePathPatterns(paths);
        }
    }

}
